package com.liveneo.adapter.mobile.web.controller;

import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.liveneo.adapter.base.entity.PrpmCaseInfo;
import com.liveneo.adapter.base.entity.PrpmLossItem;
import com.liveneo.adapter.base.entity.PrpmVideoUser;
import com.liveneo.adapter.base.support.AdapterConstants;
import com.liveneo.adapter.base.support.BaseController;
import com.liveneo.adapter.base.support.LogUtils;
import com.liveneo.adapter.mobile.model.BaseResponse;
import com.liveneo.adapter.mobile.model.dto.SelfCaseInfoDto;
import com.liveneo.adapter.mobile.model.request.CaseInfoSelfRequest;
import com.liveneo.adapter.mobile.model.request.SwitchManualRequest;
import com.liveneo.adapter.mobile.model.response.SelfAssessLossInfoResponse;
import com.liveneo.adapter.mobile.service.MobileCaseService;
import com.liveneo.adapter.mobile.service.MobileLossService;
import com.liveneo.adapter.mobile.service.MobileVideoService;
import com.liveneo.adapter.mobile.support.MobileReturnCodeSupport;
import com.liveneo.adapter.mobile.util.UUIDUtils;
import com.liveneo.system.base.utils.DateUtils;
import com.liveneo.system.base.utils.EncryptUtil;
import com.liveneo.system.base.utils.JsonUtils;

/**
 * @Description: 自助相关接口
 * @return TODO
 * @author liveneo-shuqw
 * @date 2016年6月14日 下午3:44:58
 * Copyright: Copyright (c) 2015 
 * Company:北京朗泰
 * @version 3.0
 */
@Controller
@RequestMapping("/mobile")
public class MobileSelfController extends BaseController {
    @Autowired
    private MobileCaseService  mobileCaseService;
    @Autowired
    private MobileLossService  mobileLossService;
    @Autowired
    private MobileVideoService mobileVideoService;

    /**
      * @Title: 自助端-转人工查勘
      * @Description: TODO
      * @param @param data
      * @param @return 设定文件
      * @return String 返回类型
      * @throws
     */
    @RequestMapping("/switchManual.action")
    @ResponseBody
    public String switchManual(@ModelAttribute(AdapterConstants.MOBILE_REQUEST_PARAM_NAME) String data) {
        String loguuid = UUIDUtils.getUUIDStr();
        String logTitle = "switchManual入参=" + loguuid;
        String result = null;
        BaseResponse response = null;
        try {
            SwitchManualRequest br = JsonUtils.fromJsonWithBase64(logTitle, data, SwitchManualRequest.class);
            PrpmCaseInfo prpmCaseInfo = mobileCaseService.getCaseInfoByUuid(br.getCaseUuid());
            prpmCaseInfo.setCaseStatus(4);
            // 待调用核心
            response = MobileReturnCodeSupport.buildBaseReponse(MobileReturnCodeSupport.CODE_SUCCESS, MobileReturnCodeSupport.SYS_CODE, null);
        } catch (Exception e) {
            LogUtils.error("自助端-转人工查勘系统异常,错误消息：", e);
            response = MobileReturnCodeSupport.buildBaseReponse(MobileReturnCodeSupport.CODE_INTERNAL_ERROR, MobileReturnCodeSupport.SYS_CODE, null);
        }
        result = JsonUtils.toJson(response);
        LogUtils.info("自助端-转人工查勘信息,响应结果：" + result);
        // 加密返回
        result = EncryptUtil.encodeBase64(result);
        return result;
    }

    /**
      * @Title: 自助端-获取报案信息
      * @Description: TODO
      * @param @param data
      * @param @return 设定文件
      * @return String 返回类型
      * @throws
     */
    @RequestMapping("/getCaseInfo.action")
    @ResponseBody
    public String getCaseInfo(@ModelAttribute(AdapterConstants.MOBILE_REQUEST_PARAM_NAME) String data) {
        String loguuid = UUIDUtils.getUUIDStr();
        String logTitle = "getCaseInfo入参=" + loguuid;
        String result = null;
        SelfAssessLossInfoResponse response = null;
        try {
            CaseInfoSelfRequest br = JsonUtils.fromJsonWithBase64(logTitle, data, CaseInfoSelfRequest.class);
            List<PrpmLossItem> prpmLossItemList = mobileLossService.getLossItemByMobileLossId(br.getPhoneNo(), "0");
            List<SelfCaseInfoDto> caseInfoList = new ArrayList<SelfCaseInfoDto>();
            for (PrpmLossItem prpmLossItem : prpmLossItemList) {
                SelfCaseInfoDto sf = new SelfCaseInfoDto();
                PrpmCaseInfo pc = mobileCaseService.getCaseInfoByUuid(prpmLossItem.getCaseNo());
                sf.setCaseNo(pc.getCaseNo());
                sf.setCaseStatus(pc.getCaseStatus());
                sf.setContact(pc.getContact());
                sf.setContactPhone(pc.getContactPhone());
                if (pc.getDamageDate() != null) {
                    sf.setDamageDate(DateUtils.formatDateTime(pc.getDamageDate()));
                }
                sf.setDamagePlace(pc.getDamagePlace());
                sf.setDelegateReamrk(pc.getDelegateReamrk());
                sf.setFinishCaseTimeTemp(DateUtils.formatDateTime(pc.getFinishCaseTime()));
                sf.setLicenceNo(pc.getLicenseNo());
                sf.setLossStatus(pc.getCaseStatus());
                // sf.setMobileUser(pc.getMobileUser());
                String rflag = "";// pc.getReUploadFlag() == null ? "" : pc.getReUploadFlag().toString();
                sf.setReUploadFlag(rflag);
                sf.setSurveyPalce(pc.getSurveyPlace());
                sf.setUnitCode(pc.getOrgCode());
                sf.setUserName(pc.getUserName());
                sf.setUuid(pc.getCaseInfoId());
                PrpmVideoUser pu = mobileVideoService.getVideoUserByUserCode(prpmLossItem.getSurveyorCode());
                if (pu != null) {
                    sf.setAgtID(pu.getAgtId());
                    sf.setSkillID(pu.getSkillId());
                    sf.setVcid(pu.getVcId());
                }
                caseInfoList.add(sf);
            }
            // 手机号是否正确
            if (prpmLossItemList.size() > 0) {
                response = MobileReturnCodeSupport.buildReponse(MobileReturnCodeSupport.CODE_SUCCESS, MobileReturnCodeSupport.SYS_CODE, null,
                        SelfAssessLossInfoResponse.class);
                response.setCaseInfoList(caseInfoList);
            } else {
                response = MobileReturnCodeSupport.buildReponse(MobileReturnCodeSupport.CODE_ERROR_AUTH_PHONENO_NOTEXISTS, MobileReturnCodeSupport.SYS_CODE,
                        null, SelfAssessLossInfoResponse.class);
            }
        } catch (Exception e) {
            LogUtils.error("自助端-获取报案信息系统异常,错误消息：", e);
            response = MobileReturnCodeSupport.buildReponse(MobileReturnCodeSupport.CODE_INTERNAL_ERROR, MobileReturnCodeSupport.SYS_CODE, null,
                    SelfAssessLossInfoResponse.class);
        }
        result = JsonUtils.toJson(response);
        LogUtils.info("自助端-获取报案信息,响应结果：" + result);
        // 加密返回
        result = EncryptUtil.encodeBase64(result);
        return result;
    }
}
